package com.xrit.test;

import com.xrit.constant.RedisMessageConstant;
import com.xrit.utils.QiNiuUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Iterator;
import java.util.Set;

@ContextConfiguration(locations = "classpath:spring-redis.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class testRedis {

    @Autowired
    private JedisPool jedisPool;

    @Test
    public void testDel(){
        //1.获取jedis对象
        Jedis jedis = jedisPool.getResource();
        //2.获得差集
        Set<String> sdiff = jedis.sdiff(RedisMessageConstant.SETMEAL_PIC_RESOURCES, RedisMessageConstant.SETMEAL_PIC_DB_RESOURCES);
        //3.迭代差集集合
        Iterator<String> it = sdiff.iterator();
        while(it.hasNext()){
            //3.1 获取图片名称
            String imgName = it.next();
            //3.2 七牛云删除
            QiNiuUtils.removeFiles(imgName);
            //3.3 redis中也需要删除
            jedis.srem(RedisMessageConstant.SETMEAL_PIC_RESOURCES,imgName);
        }
        jedis.close();
    }
}
